local app = require 'app'
local game = app.game.running

local ingame = 
{
    throttleable = true
}

ingame.state = 
{
    phase_id = game.util.consts.phase_nil.id,
    seen_card_ids = {},
    lord_player = nil,
    hand_card_ids = 
    {
        [1] = {},
        [2] = {},
        [3] = {},
    },

    scene = nil,
    over_detail = nil,
}

ingame.mutations = 
{
    on_phase = function(store, state, phase_id)
        state.phase_id = phase_id
        store.emit('阶段变更')
    end,

    on_restart = function(store, state)
        store.emit('流局')
    end,

    on_seen_cards = function(store, state, card_ids)
        state.seen_card_ids = card_ids
        store.emit('展示底牌')
    end,

    on_notice_lord = function(store, state, no)
        local player = game.store.room.state.playing_players[no]
        state.lord_player = player
        store.emit('通知地主')
    end,

    on_deal = function(store, state, card_ids)
        state.hand_card_ids[1] = card_ids
        for local_no = 2, 3 do
            state.hand_card_ids[local_no] = {}
            for i = 1, #card_ids do
                table.insert(state.hand_card_ids[local_no], 0)
            end
        end
        store.emit('发牌')
    end,

    req_player_bid = function(store, state, args)
        local no = args.no
        local action_ids = args.action_ids
        local time = args.time
        local player = game.store.room.state.playing_players[no]
        store.emit('请求玩家叫分', {player = player, action_ids = action_ids, time = time})
    end,

    on_player_bid_result = function(store, state, args)
        local no = args.no
        local action_id = args.action_id

        local player = game.store.room.state.playing_players[no]
        store.emit("玩家叫分", {player = player, action_id = action_id})
    end,

    req_player_handout = function(store, state, args)
        local no = args.no
        local time = args.time
        local round_first = args.round_first == 1
        local player = game.store.room.state.playing_players[no]
        store.emit('请求玩家出牌', {player = player, time = time, round_first = round_first})
    end,

    on_player_handout_result = function(store, state, args)
        local no = args.no
        local card_ids = args.card_ids
        local player = game.store.room.state.playing_players[no]
        store.emit('玩家出牌', {player = player, card_ids = card_ids})
    end,

    req_handout = function(store, state, args)
        local action_ids = args.action_ids
        local time = args.time
        local util = game.require 'util.util'
        local actions = {}
        for k, action_id in ipairs(action_ids) do
            table.insert(actions, util.id2action(action_id))
        end
        store.emit('请求自己出牌', {action_ids = action_ids, time = time})
    end,

    on_over = function(store, state, detail)
        state.over_detail = detail
        store.emit("游戏结算")
    end,

    on_game_init = function(store, state, scene)
        state.phase_id = scene.phase_id
        state.scene = scene

        if state.phase_id == game.util.consts.phase_idle.id then
            store.emit('恢复游戏场景')
            return
        end

        state.seen_card_ids = state.seen_card_ids
        if scene.lord_no ~= -1 then
            state.lord_player = game.store.room.state.playing_players[state.lord_no]
        else
            state.lord_player = nil
        end

        for local_no, t in pairs(state.hand_card_ids) do
            if local_no == 1 then
                t = scene.card_ids
            else
                t = {}
                for _, left_num in ipairs(scene.left_nums) do
                    local player = game.store.room.state.playing_players[left_num.no]
                    if local_no == player.seat.local_no then
                        for i = 1, left_num.num do
                            table.insert(t, 0)
                        end
                    end
                end
            end

            state.hand_card_ids[local_no] = t
        end
        store.emit('恢复游戏场景')
    end
}

return ingame